package cn.bqjr.eily.mybatis.spring.boot.utils;

import cn.bqjr.eily.mybatis.spring.boot.entity.PageInfoVO;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 分页工具类
 */
public class PagingUtils {

    private static String REQUEST_PAGEINFO = "REQ_PAGEINFO";

    /**
     * 自动判断该request是否需要执行分页
     * @param request
     */
    public static void pagingIfExist(HttpServletRequest request) {
        if (isPagingRequest(request)) {
            paging(getRequestPageInfo(request));
        }
    }

    /**
     * 执行分页
     * @param page
     */
    public static void paging(PageInfoVO page) {
        if (validPage(page)) {
            if (StringUtils.isNotEmpty(page.getOrderBy())) {
                PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
            } else {
                PageHelper.startPage(page.getPageNum(), page.getPageSize());
            }
        }
    }

    /**
     * 校验分页信息是否有效
     * @param page
     * @return
     */
    public static boolean validPage(PageInfoVO page) {
        return (null != page && page.getPageNum() > 0 && page.getPageSize() > 0);
    }

    /**
     * 从request中解析分页信息
     * @param request
     * @return
     */
    public static PageInfoVO populateFromRequest(HttpServletRequest request) {
        return populate(request.getParameterMap());
    }

    /**
     * 从map中解析分页信息
     * @param map
     * @return
     */
    public static PageInfoVO populateFromMap(Map<String, ? extends Object> map) {
        return populate(map);
    }

    /**
     * 解析并绑定分页信息到request
     * @param request
     * @return
     */
    public static PageInfoVO initPageInfo(HttpServletRequest request) {
        PageInfoVO page = populateFromRequest(request);
        if (validPage(page)) {
            setRequestPageInfo(request, page);
            return page;
        }
        return null;
    }

    /**
     * 绑定分页信息到request
     * @param request
     * @return
     */
    public static void setRequestPageInfo(HttpServletRequest request, PageInfoVO page) {
        request.setAttribute(REQUEST_PAGEINFO, page);
    }

    /**
     * 获取request绑定的分页信息
     * @param request
     * @return
     */
    public static PageInfoVO getRequestPageInfo(HttpServletRequest request) {
        return (PageInfoVO) request.getAttribute(REQUEST_PAGEINFO);
    }

    /**
     * 判断request是否已绑定分页信息
     * @param request
     * @return
     */
    public static boolean isPagingRequest(HttpServletRequest request) {
        Object obj = request.getAttribute(REQUEST_PAGEINFO);
        return (null != obj && obj instanceof PageInfoVO);
    }

    /**
     * 清除request绑定的分页信息
     * @param request
     */
    public static void clearPageInfo(HttpServletRequest request) {
        setRequestPageInfo(request, null);
    }

    /**
     * 从map中解析分页信息
     * @param map
     * @return
     */
    private static PageInfoVO populate(Map<String, ? extends Object> map) {
        PageInfoVO page = new PageInfoVO();
        try {
            BeanUtils.populate(page, map);
            return page;
        } catch (Exception e) {
            return null;
        }
    }
}
